package com.yt.user.server;

import com.yt.db.config.DataSourceConfiguration;
import com.yt.db.config.RedisConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;

@SpringBootApplication
@Import({DataSourceConfiguration.class, RedisConfig.class})
@ComponentScan(basePackages = {"com.yt.user.server", "com.yt.sys.server"},
        excludeFilters = {
                @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = {com.yt.sys.server.common.config.Swagger2.class}),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.yt.sys.server.common.config.YmlConfig"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.yt.sys.server.config.GlobalExceptionHandler"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.yt.claim.server.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.yt.Instrance.server.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.yt.product.server.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.yt.setclaim.server.*"),
                // 排除sys-server中的控制器和服务实现类
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.yt.sys.server.api.*"),
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.yt.sys.server.service.impl.*"),
                // 排除sys-server中的Mapper类，避免重复扫描
                @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.yt.sys.server.mapper.*")
        })
public class userApplication {
    public static void main(String[] args) {
        // 解决 Microsoft JDK extnet 错误
        System.setProperty("jdk.net.ext.check", "false");
        System.setProperty("jdk.net.usePlainSocketServer", "true");
        
        SpringApplication.run(userApplication.class, args);
        System.out.println("User Service Started!");
    }
    
    /**
     * 自定义Mapper Bean名称生成器，避免bean名称冲突
     */
    public static class MapperNameGenerator extends AnnotationBeanNameGenerator {
        @Override
        protected String buildDefaultBeanName(BeanDefinition definition) {
            String beanClassName = definition.getBeanClassName();
            if (beanClassName != null) {
                // 为所有Mapper类生成唯一的Bean名称
                if (beanClassName.endsWith("Mapper")) {
                    // 使用完整的包名+类名作为Bean名称，确保唯一性
                    return beanClassName.replace(".", "_");
                }
            }
            return super.buildDefaultBeanName(definition);
        }
        
        @Override
        public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) {
            String beanClassName = definition.getBeanClassName();
            if (beanClassName != null && beanClassName.endsWith("Mapper")) {
                // 对于Mapper接口，使用全限定类名作为Bean名称
                return beanClassName.replace(".", "_");
            }
            return super.generateBeanName(definition, registry);
        }
    }
}